package myschool.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
@WebFilter(filterName = "timer", value = "/*")
public class TimeFilter implements Filter {
    public TimeFilter() {
        System.out.println("TimeFilter加载了");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化TimeFiler");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入了TimeFilter");
        long start = System.nanoTime();
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String path = request.getServletPath();
        System.out.println("请求地址：" + path);
        filterChain.doFilter(request,servletResponse);//!!!!!!!!!!!!
        long end=System.nanoTime();
        System.out.println(path+" 耗时"+(end-start)+"纳秒");

        System.out.println("离开了TimeFilter");
    }

    @Override
    public void destroy() {
        System.out.println("TimeFilter销毁了");
    }
}
